package com.xzy.d;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 说明：
 * 1、通过BeanDefinition定义Bean
 * 2、将BeanDefinition注册到Spring容器
 * 3、Spring容器依据BeanDefinition创建Bean
 *
 * @author xzy
 * @date 2021/11/28 17:43
 */
public class Main {

    public static void main(String[] args) {
        method0();
        method1();
    }

    public static void method0() {
        // 1、通过BeanDefinition定义Bean
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition.setBeanClass(HumanServiceImpl.class);

        // 2、将BeanDefinition注册到Spring容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.registerBeanDefinition("humanServiceImpl", beanDefinition);
        applicationContext.refresh();

        // 3、Spring容器依据BeanDefinition创建Bean
        HumanService bean = applicationContext.getBean(HumanService.class);
        System.out.println(bean);
    }

    public static void method1() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 查看registerBean方法的源码可以发现，最终还是通过BeanDefinition定义Bean
        applicationContext.registerBean(HumanServiceImpl.class);
        applicationContext.refresh();

        HumanService bean = applicationContext.getBean(HumanService.class);
        System.out.println(bean);
    }
}